#pragma once
#include <d3d9.h>
#include <d3dx9.h>

#include "interfaces.h"
#include "Mesh.h"
#include "Effect.h"
#include "Texture.h"
class WindowsFramework;
class Renderer9 : public IRenderer
{
public:
	friend WindowsFramework;
	Renderer9() { m_pREffect = NULL; m_pTexture = NULL; m_pMaterial = NULL; }
	~Renderer9() { 

	}

//-----------------------------------------------------------------------------------------------------------------
//Device Functions
//-----------------------------------------------------------------------------------------------------------------
	virtual void VValidateDevice(unsigned long num_passes);
	virtual void VBeginScene(); //1
	virtual void VEndScene(); //1
	virtual void VShowCursor(const bool show_cursor); //1
	virtual void VResetDeviceState( U32 width, U32 height);
	virtual bool VPresentBackBuffer();
	virtual RE_DEVSTATE VRequestState();
//-----------------------------------------------------------------------------------------------------------------
//Buffer Functions
//-----------------------------------------------------------------------------------------------------------------
	virtual void VDrawMesh(RE_Mesh * mesh);
	virtual void VUpdateIndexBuffer(RE_Mesh * mesh);
	virtual void VUpdateVertexBuffer(RE_Mesh * mesh);
	virtual bool VReleaseBuffers(RE_Mesh * mesh);
	virtual void VSetSamplerState();
//-----------------------------------------------------------------------------------------------------------------
//Matrix States
//-----------------------------------------------------------------------------------------------------------------
	virtual void VSetWorldMatrix(const Mat4x4 * const world);
	virtual const Mat4x4* VGetWorldMatrix();
	virtual void VSetViewMatrix(const Mat4x4 * const view);
	virtual const Mat4x4* VGetViewMatrix();
	virtual void VSetProjMatrix(const Mat4x4 * const proj);
	virtual const Mat4x4* VGetProjMatrix();
//-----------------------------------------------------------------------------------------------------------------
//Material States
//-----------------------------------------------------------------------------------------------------------------
	virtual void VSetMaterial(RE_Material * mat);
	virtual RE_Material* const VGetMaterial();

	virtual void VCreateEffect(RE_Effect * effect);
	virtual void VSetEffect(RE_Effect * effect);
	virtual RE_Effect* VGetEffect();
	virtual void VReleaseEffect(RE_Effect * effect);


	virtual void VSetDeclaration(D3DVERTEXELEMENT9 * vd);
	virtual D3DVERTEXELEMENT9* VGetDeclaration();

	virtual void VCreatePixelShader();
	virtual void VCreateVertexShader();
	virtual void VSetPixelShader();
	virtual void VGetPixelShader();
	virtual void VSetVertexShader();
	virtual void VGetVertexShader();
//-----------------------------------------------------------------------------------------------------------------
//Render Targets And Surfaces
//-----------------------------------------------------------------------------------------------------------------
	virtual void VClear();
	virtual void VSetColorFill();
	virtual void TakeScreenshot(std::wstring location);
	virtual void VCreateAdditionalSwapChain();
	virtual void VCreateCubeTextureSurface();
	virtual void VCreateDepthStencilSurface();
	virtual void VCreateOffscreenPlainSurface();
	virtual void VCreateRenderTarget(); 
	virtual void VUpdateSurface(); // copy portion of one texture to another
	virtual void VStretchRectangle();
//-----------------------------------------------------------------------------------------------------------------
//Textures
//-----------------------------------------------------------------------------------------------------------------
	virtual void VUpdateTexture(RE_Texture * texture);
	virtual void VReleaseTexture(RE_Texture * texture);
	virtual void VSetTexture( RE_Texture * texture );
//-----------------------------------------------------------------------------------------------------------------
//Variables
//-----------------------------------------------------------------------------------------------------------------
private:
	D3DXMATRIXA16 m_World, m_View, m_Proj;
	RE_Effect* m_pREffect; // main rendering effect
	RE_Texture* m_pTexture; // current texture to use
	RE_Material * m_pMaterial; // current material to send
	unsigned int VertexElementSize;
	LPDIRECT3D9			m_pd3d;
	LPDIRECT3DDEVICE9	m_pd3d9Device;
//Special funcs
protected:
	bool Initialize(HWND hwnd);

};